package com.mybatis;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 1.实现TypeHandler接口,或者BaseTypeHandler
 *
 */
public class MyEnumEmpStatusTypeHandler implements TypeHandler<EmpStatus>{

	/**
	 * 定义当前数据如何保存到数据库中
	 * @param ps
	 * @param i
	 * @param parameter
	 * @param jdbcType
	 * @throws SQLException
	 */
	@Override
	public void setParameter(PreparedStatement ps, int i, EmpStatus parameter, JdbcType jdbcType) throws SQLException {
		ps.setString(i,parameter.getCode().toString());
	}

	@Override
	public EmpStatus getResult(ResultSet rs, String columnName) throws SQLException {
		//需要根据从数据库中拿到的状态码返回对应的枚举类型
		int anInt = rs.getInt(columnName);
		return EmpStatus.getEmpStatusByCode(anInt);
	}

	@Override
	public EmpStatus getResult(ResultSet rs, int columnIndex) throws SQLException {
		//需要根据从数据库中拿到的状态码返回对应的枚举类型
		int anInt = rs.getInt(columnIndex);
		return EmpStatus.getEmpStatusByCode(anInt);
	}

	@Override
	public EmpStatus getResult(CallableStatement cs, int columnIndex) throws SQLException {
		int anInt = cs.getInt(columnIndex);
		return EmpStatus.getEmpStatusByCode(anInt);
	}
}
